package ac.kr.pusan.dj.han.dic.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ac.kr.pusan.dj.han.dic.model.bean.CharMainBean;

/**
 * @author guy
 *
 */
public class CharMainDAO {
	private ArrayList<CharMainBean> beanPod;	// 검색 결과 목록 (한글자 혹은 여러자)
	private boolean flag;						// 검색 성공 여부 flag
	
	private Connection con;
	private PreparedStatement pstmt;
	

	
	public CharMainDAO() {
		beanPod = new ArrayList<CharMainBean>();
	}
	
	//-- 결과 목록(한자 여러개)을 넘겨준다.
	public ArrayList<CharMainBean> getBeanPod() {
//		String uni_code, String um, String hun, String radical,
//		int count,
//		String sub1, String sub2, String sub3, String sub4)
		return beanPod;
	}
	
	/*
	 * 문자 검색하기
	 * 0 : 결과 없음 (Fail)
	 * 1 : 검색 성공 (Success)
	 */
	public boolean searchChar(String sChar) {

		try {
			flag = executeSQL();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return flag;
	}

	//-- SQL 실행 --------------------------------------------------------
	private boolean executeSQL() throws SQLException {

		// 1. con 생성
		// 2. pstmt
		String sql="select * from member";
		ResultSet rs=null;
		con=DBConnection.getCon();
		sql = setQuery();			// <<
		
		pstmt=con.prepareStatement(sql);
		rs=pstmt.executeQuery(); //쿼리실행을위한 메소드
		if(rs.isLast()) {
		while(rs.next()) {
			//현재행의 컬럼값들 얻어오기
				String uni_code=rs.getString("uni_code");
				String um=rs.getString("um");
				String hun=rs.getString("hun");
				String radical=rs.getString("radical");
				int count=rs.getInt("count");
				String sub1=rs.getString("sub1");
				String sub2=rs.getString("sub2");
				String sub3=rs.getString("sub3");
				String sub4=rs.getString("sub4");
//				Date regdate=rs.getDate("regdate");
				//현재행의 컬럼값들을 VO객체애 담기
				CharMainBean bean = new CharMainBean(uni_code, um, hun, radical, count, sub1, sub2, sub3, sub4);
				//VO객체를 ArrayList배열에 담기
				beanPod.add(bean);	
			}
		} else {
			return false;
		}
		return true;
	}

	private String setQuery() {
		// TODO SQL문 선택
		return "select * from member";
	}
}
